package com.insurance.modules.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.insurance.modules.sys.controller.AbstractController;
import com.insurance.modules.sys.entity.SysUserEntity;
import com.insurance.producttype.entity.ProductTypeEntity;
import com.insurance.producttype.enums.ProductTypeStateEnum;
import com.insurance.producttype.service.ProductTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.R;



/**
 * 产品类别
 *
 * @author hikari
 * @email devilzwy@gmail.com
 * @date 2020-09-16 22:01:28
 */
@RestController
@RequestMapping("product/producttype")
public class ProductTypeController extends AbstractController {

    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list/{parentId}")
    @RequiresPermissions("product:producttype:list")
    public R list(@PathVariable String parentId, String name, @RequestParam Map<String, Object> params){
        PageUtils page = productTypeService.queryPage(params, parentId, name);
        return R.ok().put("page", page);
    }

    /**
     * 查询产品类型树结构
     *
     * @return
     */
    @RequestMapping("/tree")
    @RequiresPermissions("product:producttype:list")
    public R tree(){
        List<ProductTypeEntity> productTypeTree = productTypeService.findProductTypeTree();
        return R.ok().put("productTypeTree", productTypeTree);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:producttype:info")
    public R info(@PathVariable("id") Long id){
		ProductTypeEntity productType = productTypeService.getById(id);
        return R.ok().put("productType", productType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:producttype:save")
    public R save(@RequestBody ProductTypeEntity productType){
        SysUserEntity user = getUser();
        productType.setCreatePersonId(String.valueOf(user.getUserId()));
        productType.setCreatePersonName(user.getUsername());
        productType.setCreateTime(new Date());
        productType.setState(ProductTypeStateEnum.UP.getCode());
        productType.setDeleteFlag("0");
		productTypeService.save(productType);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:producttype:update")
    public R update(@RequestBody ProductTypeEntity productType){
        SysUserEntity user = getUser();
        productType.setUpdatePersonId(String.valueOf(user.getUserId()));
        productType.setUpdatePersonName(user.getUsername());
        productType.setUpdateTime(new Date());
		productTypeService.updateById(productType);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:producttype:delete")
    public R delete(@RequestBody Long[] ids){
		productTypeService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }




}
